<template>
    <div class="system-dept-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">
            <div class="system-dept-search mb15">
                <el-input size="default" placeholder="请输入部门名称" style="max-width: 180px"></el-input>
                <el-button size="default" type="primary" class="ml10">
                    <el-icon>
                        <ele-Search/>
                    </el-icon>
                    查询
                </el-button>
                <el-button size="default" type="success" class="ml10" @click="onOpenAddDept('add')">
                    <el-icon>
                        <ele-FolderAdd/>
                    </el-icon>
                    新增部门
                </el-button>
            </div>
            <el-table
                    :data="state.tableData.data"
                    v-loading="state.tableData.loading"
                    style="width: 100%;margin-bottom: 20px"
                    row-key="deptId"
                    default-expand-all
                    :tree-props="{ children: 'children' }"
            >
                <el-table-column align="center" type="selection" width="55"/>
                <el-table-column align="center" type="index" label="序号" width="80"/>
                <el-table-column align="center" prop="name" label="机构名称" width="150" show-overflow-tooltip/>
                <el-table-column align="center" prop="deptCode" label="机构编码" show-overflow-tooltip/>
                <el-table-column align="center" prop="deptDesc" label="部门描述" show-overflow-tooltip/>
                <el-table-column align="center" label="操作" width="220">
                    <template #default="scope">
                        <el-button size="small" text type="primary" @click="onOpenAddDept('addChild',scope.row)">新增
                        </el-button>
                        <el-button size="small" text type="primary" @click="onOpenEditDept('edit', scope.row)">修改
                        </el-button>
                        <el-button size="small" text type="primary" @click="onTableRowDel(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <DeptDialog ref="deptDialogRef" @refresh="getTableData()"/>
    </div>
</template>

<script setup lang="ts" name="systemDept">
import {defineAsyncComponent, ref, reactive, onMounted} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import DeptApi from "/@/api/system/DeptApi";

// 引入组件
const DeptDialog = defineAsyncComponent(() => import('/@/views/system/dept/dialog.vue'));

// 定义变量内容
const deptDialogRef = ref();
const state = reactive<SysDeptState>({
    tableData: {
        data: [],
        total: 0,
        loading: false,
        param: {
            pageNum: 1,
            pageSize: 10,
        },
    },
});

// 初始化表格数据
const getTableData = () => {
    state.tableData.loading = true;
    DeptApi.getList(state.tableData.param).then(res => {
        state.tableData.data = [];
        state.tableData.data = res.data
    }).finally(() => {
        state.tableData.loading = false;
    })
};
// 打开新增菜单弹窗
const onOpenAddDept = (type: string, row: DeptTreeType) => {
    deptDialogRef.value.openDialog(type, row);
};
// 打开编辑菜单弹窗
const onOpenEditDept = (type: string, row: DeptTreeType) => {
    deptDialogRef.value.openDialog(type, row);
};
// 删除当前行
const onTableRowDel = (row: DeptTreeType) => {
    ElMessageBox.confirm(`此操作将永久删除部门：${row.name}, 是否继续?`, '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
    })
        .then(() => {
            getTableData();
            ElMessage.success('删除成功');
        })
        .catch(() => {
        });
};
// 页面加载时
onMounted(() => {
    getTableData();
});
</script>
